import React, {Component} from 'react';
import {View,Text,TouchableOpacity,StyleSheet,DeviceEventEmitter,Button} from 'react-native'
import AsyncStorage from "@react-native-community/async-storage";

class Order extends Component {
  constructor(props){
    super(props);
    this.state = {
      login: false  // 判断是否登录
    }
  }
  render() {
    let {login} = this.state;
    if (login) {
      return <View style={{flex:1}}>
        <Text style={styles.top}>订单页</Text>
        <View style={{justifyContent:'center',alignItems:'center',flex:1}}>
          <Text style={{color:"red"}}>订单列表</Text>
        </View>
      </View>
    }
    return (
      <View style={styles.box}>
        <Text style={styles.top}>未完成</Text>
        <View style={styles.login}>
          <Button title={'刷新'} onPress={this.refreshLogin}/>
          <Text style={styles.loginTxt}>您还没有登录，请先登录</Text>
          <TouchableOpacity style={styles.loginBtn} onPress={this.goLogin}>
            <Text style={styles.loginBtnTxt}>登录</Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }


  componentDidMount() {
    // this.subscription = DeviceEventEmitter.addListener('UPDATE_USER_DATA', this.refreshData);
    AsyncStorage.removeItem('login');
    AsyncStorage.removeItem('token');
    // 检查用户是否登录
    AsyncStorage.getItem('login',(err,data)=>{
      // console.warn('data1',err,data);
      if (data) {
        this.setState({
          login:true
        })
      }
    });
  }

  // componentWillUnmount() {
  //   this.subscription.remove();
  // };

  // 主动刷新
  refreshLogin() {
    console.log(this.props)
  };

  goLogin = () => {
    this.props.navigation.navigate('login');
  }
}

const styles=StyleSheet.create({
  box:{
    backgroundColor: '#fff',
    flex:1,
  },
  top:{
    backgroundColor:'#1fc9ff',
    height:80,
    lineHeight:80,
    textAlign:'center',
    fontSize:18,
    color:'#fff',
  },
  login:{
    flex:1,
    justifyContent:"center",
    alignItems:'center',
  },
  loginTxt:{
    color:'#666',
    marginBottom: 40,
  },
  loginBtn:{
    width:140,
    height:40,
    borderRadius:20,
    backgroundColor:'#1fc9ff',
    justifyContent: 'center',
    alignItems: 'center',
  },
  loginBtnTxt:{
    color:'#fff',
  }
});

export default Order;